package wzj.mybatisplusdemo.config;

import io.modelcontextprotocol.client.McpSyncClient;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import wzj.mybatisplusdemo.tools.CreationSqlTool;

import java.util.List;

@Configuration
public class AiConfig {
    @Bean
    public ChatMemory chatMemory() {
        ChatMemory chatMemory = MessageWindowChatMemory.builder()
                .maxMessages(5)
                .build();
        return chatMemory;
    }
    @Bean
    public ChatClient chatClientTxt(OpenAiChatModel openAiChatModel, ChatMemory chatMemory) {
        return ChatClient.builder(openAiChatModel)
                .defaultSystem("你是一个小说家，根据作者的意图，编写小说，字数限制在1000字以内")
                .defaultAdvisors(
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                        ,new SimpleLoggerAdvisor())
                .build();
    }
    @Bean
    public ChatClient aiManager(OpenAiChatModel openAiChatModel, ChatMemory chatMemory) {
        return ChatClient.builder(openAiChatModel)
                .defaultSystem("你是一个ai管理助手，根据管理员的指示，自动化操作数据库，调用魔塔mcp")
                .defaultAdvisors(
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                        ,new SimpleLoggerAdvisor())
                .build();
    }
    @Bean
    public ChatClient chatClientPoem(OpenAiChatModel openAiChatModel, ChatMemory chatMemory) {
        return ChatClient.builder(openAiChatModel)
                .defaultSystem("你是一个诗人，根据用户的意图，编写诗词，字数限制在1000字以内")
                .defaultAdvisors(
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                        ,new SimpleLoggerAdvisor()
                )
                .build();
    }
    @Bean
    public ChatClient chatClientHappy(OpenAiChatModel openAiChatModel, ChatMemory chatMemory) {
        return ChatClient.builder(openAiChatModel)
                .defaultSystem("你是一个有创意的祝福语语言学家，根据作者的主题，编写祝福语，有趣得当")
                .defaultAdvisors(
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                        ,new SimpleLoggerAdvisor()
                )
                .build();
    }
    @Autowired
    private List<McpSyncClient> mcpSyncClients;

    @Autowired
    private CreationSqlTool creationSqlTool;
    @Bean
    public ChatClient asistantClient(OpenAiChatModel openAiChatModel, ChatMemory chatMemory) {
        return ChatClient.builder(openAiChatModel)
                .defaultSystem("你是一个管理员小助手，不仅可以帮助管理员发送对应user的邮件，还可以帮管理员管理creations表，" +
                        "你是一个数据库管理员助手，专注于管理creations表。你可以自动生成并执行SQL语句。\n" +
                        "===== 表结构信息 =====\n" +
                        "表名: creations (存储文章信息)\n" +
                        "字段说明:\n" +
                        "- s_id: 整数，主键，文章唯一ID\n" +
                        "- s_title: 字符串，文章标题\n" +
                        "- s_content: 字符串，文章内容\n" +
                        "- s_img: 字符串，图片路径\n" +
                        "- s_click: 整数，点击量\n" +
                        "- s_userid: 整数，发布用户ID\n" +
                        "- s_cgy: 整数，分类ID\n" +
                        "- s_keyword: 字符串，关键词\n" +
                        "- s_createtime: 日期时间，创建时间\n" +
                        "\n" +
                        "===== 操作流程 =====\n" +
                        "1. 先调用getCreationTableStructure()确认最新表结构\n" +
                        "2. 根据用户需求生成符合MySQL语法的SQL语句\n" +
                        "3. 执行前检查SQL是否只操作creations表，不包含危险语句\n" +
                        "4. 使用executeCreationQuery()执行查询，使用executeCreationUpdate()执行增删改\n" +
                        "\n" +
                        "===== 注意事项 =====\n" +
                        "- 字符串值需要用单引号包裹，特殊字符要转义\n" +
                        "- 日期格式使用'yyyy-MM-dd HH:mm:ss'\n" +
                        "- 删除和更新操作必须包含WHERE条件\n" +
                        "- 复杂操作先询问用户确认")
                .defaultAdvisors(
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                        ,new SimpleLoggerAdvisor()
                )
                .defaultTools(creationSqlTool)
                .defaultToolCallbacks(new SyncMcpToolCallbackProvider(mcpSyncClients))
                .build();
    }
}
